package com.ylkj.patient.newpickview;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.yilijk.base.utils.ALog;
import com.ylkj.patient.MyActivityManager;
import com.ylkj.patient.R;
import com.ylkj.patient.pickview.WheelView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map;

public class PickViewUtils implements View.OnClickListener {
    private final String MARK = "mark";
    private final String title = "title";
    private final String selInfo = "selInfo";
    private String TYPE = "";
    private String[] weeks = {"每周一", "每周二", "每周三", "每周四", "每周五", "每周六", "每周日"};
    private String[] times = {
            "00:00",
            "00:30",
            "01:00",
            "01:30",
            "02:00",
            "02:30",
            "03:00",
            "03:30",
            "04:00",
            "04:30",
            "05:00",
            "05:30",
            "06:00",
            "06:30",
            "07:00",
            "07:30",
            "08:00",
            "08:30",
            "09:00",
            "09:30",
            "10:00",
            "10:30",
            "11:00",
            "11:30",
            "12:00",
            "12:30",
            "13:00",
            "13:30",
            "14:00",
            "14:30",
            "15:00",
            "15:30",
            "16:00",
            "16:30",
            "17:00",
            "17:30",
            "18:00",
            "18:30",
            "19:00",
            "19:30",
            "20:00",
            "20:30",
            "21:00",
            "21:30",
            "22:00",
            "22:30",
            "23:00",
            "23:30",
    };

    private static PickViewUtils pickViewUtils = new PickViewUtils();

    static Calendar calendar_ymd_hm;
    //获取系统的日期
    //年
    static int year;
    //月
    static int month;
    //日
    static int day;
    //获取系统时间
    //小时
    static int hour;
    //分钟
    static int minute;

    public static PickViewUtils getPickViewUtils() {
        getCurrentTime();
        return pickViewUtils;
    }

    static void getCurrentTime(){
        calendar_ymd_hm = Calendar.getInstance();
        //获取系统的日期
        //年
        year = calendar_ymd_hm.get(Calendar.YEAR);
        //月
        month = calendar_ymd_hm.get(Calendar.MONTH) + 1;
        //日
        day = calendar_ymd_hm.get(Calendar.DAY_OF_MONTH);
        //获取系统时间
        //小时
        hour = calendar_ymd_hm.get(Calendar.HOUR_OF_DAY);
        //分钟
        minute = calendar_ymd_hm.get(Calendar.MINUTE);
    }


    private PopupWindow popupWindow;
    private TextView pickviewCancel;
    private TextView pickviewTitle;
    private TextView pickviewSure;
    private LinearLayout selectType;
    private View popupWindowView;

    //pickview创建
    public void pickViewCreate(Activity activity) {
        popupWindow = new PopupWindow(activity);
        popupWindowView = LayoutInflater.from(activity).inflate(R.layout.pickview_popwindow, null);
        //pickviewCancel pop取消
        pickviewCancel = (TextView) popupWindowView.findViewById(R.id.pickview_cancel);
        //pickviewCancel pop标题
        pickviewTitle = (TextView) popupWindowView.findViewById(R.id.pickview_title);
        //pickviewCancel pop确定
        pickviewSure = (TextView) popupWindowView.findViewById(R.id.pickview_sure);
        //类型布局
        selectType = (LinearLayout) popupWindowView.findViewById(R.id.select_type);
        pickviewCancel.setOnClickListener(this);
        pickviewSure.setOnClickListener(this);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1.0f);
            }
        });

    }

    /**
     * 调用展示pickview
     *
     * @param activity         当前activity
     * @param pickViewCallback pickview回调
     * @param type             显示pickview类型
     * @param title            pickview标题
     * @param cancelText       退出文案
     * @param sureText         确定文案
     * @param currentTime      当前时间 没有传空
     * @param map              是否是自定义数据 没有传null
     * @param decorView        当前视图view
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void getPickViewShow(Activity activity,
                                String type, String title, String cancelText, String sureText, String currentTime, Map<String, Object> map, View decorView
            , PickViewCallback pickViewCallback) {
        pickViewCreate(activity);
        showPickViewAndTYPE(activity, type, title, cancelText, sureText, currentTime, map, decorView, pickViewCallback);

    }


    private void setBackgroundAlpha(float v) {
        Window window = MyActivityManager.getInstance().getCurrentActivity().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        lp.alpha = v; //0.0-1.0
        window.setAttributes(lp);
    }


    //设置pickView 标题
    private void setPickviewTitle(String title) {
        pickviewTitle.setText(title);
    }

    //设置pickView 退出
    private void setPickviewCancel(String cancel) {
        pickviewCancel.setText(cancel);
    }

    //设置pickView 确定
    private void setPickviewSure(String sure) {
        pickviewSure.setText(sure);
    }


    private final String Type_WEEKTIME = "weekTime";
    private final String Type_TIME = "time";
    private final String Type_TIME_HM = "time_HM";
    private final String Type_DATATIME = "dateTime";
    private final String Type_NORMAL = "normal";
    private final String Type_TIMEINTERVAL = "timeInterval";
    private final String Type_MORETIME = "moreTime";

    /**
     * pickview week
     */
    private WheelView weekTime;
    private WheelView weekstartTime;
    private WheelView weekstopTime;
    private WheelView splitView;

    /**
     * Time X 4
     */
    private WheelView time4StartTime1;
    private WheelView time4SplitView1;
    private WheelView time4StopTime1;
    private WheelView time4StartTime2;
    private WheelView time4SplitView2;
    private WheelView time4StopTime2;

    /**
     * 00:00 至00：00
     */
    private WheelView intervalstartTime;
    private WheelView intervalsplitView;
    private WheelView intervalstopTime;

    /**
     * datatime
     */
    private WheelView dataTime;
    private WheelView datastartTime;
    private WheelView datastopTime;
    private WheelView datasplitView;

    /**
     * normal
     */
    private WheelView normalTime;

    /**
     * year
     */
    /**
     * year
     */
    private WheelView yearTime;
    private WheelView yearSplit;
    private WheelView monthTime;
    private WheelView monthSplit;
    private WheelView dayTime;
    private WheelView daySplit;
    private ArrayList<Integer> years = new ArrayList<>();
    private ArrayList<Integer> months = new ArrayList<>();
    private ArrayList<Integer> days = new ArrayList<>();

    /**
     * 年月日时分
     */
    private WheelView timeYear;
    private WheelView timeMonth;
    private WheelView timeDay;
    private WheelView timeHour;
    private WheelView timeMinutes;


    //设置pickview 类型
    public void selectPickViewType(Activity activity, String type, String currentTime, Map<String, Object> map) {
        TYPE = type;
        //选择pickview类型
        selectType.removeAllViews();
        switch (type) {
            case Type_TIME_HM:
                View pickview_ymd_hm = LayoutInflater.from(activity).inflate(R.layout.pickview_ymd_hms, null);
                timeYear = (WheelView) pickview_ymd_hm.findViewById(R.id.time_year);
                timeMonth = (WheelView) pickview_ymd_hm.findViewById(R.id.time_month);
                timeDay = (WheelView) pickview_ymd_hm.findViewById(R.id.time_day);
                timeHour = (WheelView) pickview_ymd_hm.findViewById(R.id.time_hour);
                timeMinutes = (WheelView) pickview_ymd_hm.findViewById(R.id.time_minutes);

                timeYear.setPickViewScrollApi(new PickViewScrollApi() {
                    @Override
                    public void computeScroll() {
                        ALog.e("timeYear滚动完成" + (String) timeYear.getCenterItem());
                        if ((1970 + "年").equals((String) timeYear.getCenterItem())) {
                            timeYear.setCenterItem(0);
                        }
                        if ((year + "年").equals((String) timeYear.getCenterItem())) {
                            timeYear.setCenterItem(year - 1970);
                            refreshTimePosition(false, true, true, true, true,
                                    false, true, true, true, true,
                                    timeMonth, timeDay, timeHour, timeMinutes);
                        } else {
                            refreshTimePosition(false, false, false, false, false,
                                    false, true, true, true, true,
                                    timeMonth, timeDay, timeHour, timeMinutes);
                        }
                    }
                });
                timeMonth.setPickViewScrollApi(new PickViewScrollApi() {
                    @Override
                    public void computeScroll() {
                        ALog.e("timeMonth滚动完成" + (String) timeMonth.getCenterItem());
                        if ((String.format("%02d", 1) + "月").equals((String) timeMonth.getCenterItem())) {
                            timeMonth.setCenterItem(0);
                        }
                        if ((12 + "月").equals((String) timeMonth.getCenterItem())) {
                            timeMonth.setCenterItem(11);
                        }
                        if ((year + "年").equals((String) timeYear.getCenterItem()) &&
                                (String.format("%02d", month) + "月").equals((String) timeMonth.getCenterItem())) {
                            timeMonth.setCenterItem(month - 1);
                            refreshTimePosition(false, false, true, true, true,
                                    false, false, true, true, true,
                                    timeDay, timeHour, timeMinutes);
                        } else {
                            refreshTimePosition(false, false, false, false, false,
                                    false, false, true, true, true,
                                    timeDay, timeHour, timeMinutes);
                        }
                    }
                });
                timeDay.setPickViewScrollApi(new PickViewScrollApi() {
                    @Override
                    public void computeScroll() {
                        ALog.e("timeDay滚动完成" + (String) timeDay.getCenterItem());
                        if ((String.format("%02d", 1) + "日").equals((String) timeDay.getCenterItem())) {
                            timeDay.setCenterItem(0);
                        }
                        //获取当前年y
                        //获取当前月
                        String ycenterItem = (String) timeYear.getCenterItem();
                        String[] yArray = ycenterItem.split("年");
                        int yi = Integer.valueOf(yArray[0]).intValue();

                        boolean isFour = yi % 4 == 0 ? true : false;

                        //获取当前月
                        String mcenterItem = (String) timeMonth.getCenterItem();
                        String[] monthArray = mcenterItem.split("月");
                        int mi = Integer.valueOf(monthArray[0]).intValue();

                        if (isFour && mi == 2) {
                            if ((String.format("%02d", 29) + "日").equals((String) timeDay.getCenterItem())) {
                                timeDay.setCenterItem(28);
                            }
                        } else if (mi == 2) {
                            if ((String.format("%02d", 28) + "日").equals((String) timeDay.getCenterItem())) {
                                timeDay.setCenterItem(27);
                            }
                        } else if (mi == 1 || mi == 3 || mi == 5 || mi == 7 || mi == 8 || mi == 10 || mi == 12) {
                            if ((String.format("%02d", 31) + "日").equals((String) timeDay.getCenterItem())) {
                                timeDay.setCenterItem(30);
                            }
                        } else if (mi == 4 || mi == 6 || mi == 9 || mi == 11) {
                            if ((String.format("%02d", 30) + "日").equals((String) timeDay.getCenterItem())) {
                                timeDay.setCenterItem(29);
                            }
                        }

                        if ((year + "年").equals((String) timeYear.getCenterItem()) &&
                                (String.format("%02d", month) + "月").equals((String) timeMonth.getCenterItem()) &&
                                (String.format("%02d", day) + "日").equals((String) timeDay.getCenterItem())) {
                            timeDay.setCenterItem(day - 1);
                            refreshTimePosition(false, false, false, true, true,
                                    false, false, false, true, true,
                                    timeHour, timeMinutes);
                        } else {
                            refreshTimePosition(false, false, false, false, false,
                                    false, false, false, true, true,
                                    timeHour, timeMinutes);
                        }

                    }
                });
                timeHour.setPickViewScrollApi(new PickViewScrollApi() {
                    @Override
                    public void computeScroll() {
                        ALog.e("timeHour滚动完成" + (String) timeHour.getCenterItem());
                        if ((String.format("%02d", 0) + "时").equals((String) timeHour.getCenterItem())) {
                            timeHour.setCenterItem(0);
                        }
                        if ((23 + "时").equals((String) timeHour.getCenterItem())) {
                            timeHour.setCenterItem(23);
                        }
                        if ((year + "年").equals((String) timeYear.getCenterItem()) &&
                                (String.format("%02d", month) + "月").equals((String) timeMonth.getCenterItem()) &&
                                (String.format("%02d", day) + "日").equals((String) timeDay.getCenterItem()) &&
                                (String.format("%02d", hour) + "时").equals((String) timeHour.getCenterItem())
                        ) {
                            timeHour.setCenterItem(hour);
                            refreshTimePosition(false, false, false, false, true,
                                    false, false, false, false, true,
                                    timeMinutes);
                        } else {
                            refreshTimePosition(false, false, false, false, false,
                                    false, false, false, false, true,
                                    timeMinutes);
                        }

                    }
                });
                timeMinutes.setPickViewScrollApi(new PickViewScrollApi() {
                    @Override
                    public void computeScroll() {
                        ALog.e("timeMinutes滚动完成" + (String) timeMinutes.getCenterItem());
                        if ((String.format("%02d", 0) + "分").equals((String) timeMinutes.getCenterItem())) {
                            timeMinutes.setCenterItem(0);
                        }
                        if ((59 + "分").equals((String) timeMinutes.getCenterItem())) {
                            timeMinutes.setCenterItem(59);
                        }
                        if ((year + "年").equals((String) timeYear.getCenterItem()) &&
                                (String.format("%02d", month) + "月").equals((String) timeMonth.getCenterItem()) &&
                                (String.format("%02d", day) + "日").equals((String) timeDay.getCenterItem()) &&
                                (String.format("%02d", hour) + "时").equals((String) timeHour.getCenterItem()) &&
                                (String.format("%02d", minute) + "分").equals((String) timeMinutes.getCenterItem())
                        ) {
                            timeMinutes.setCenterItem(minute);
                        }
                    }
                });

                ALog.e(year + "");
                ALog.e(month + "");
                ALog.e(day + "");
                ALog.e(hour + "");
                ALog.e(minute + "");
                refreshTimePosition(true, true, true, true, true,
                        true, true, true, true, true,
                        timeYear, timeMonth, timeDay, timeHour, timeMinutes);

//                timeYear.

                selectType.addView(pickview_ymd_hm);
                break;
            case Type_WEEKTIME://周  开始时间 结束时间
                View pickview_week = LayoutInflater.from(activity).inflate(R.layout.pickview_week, null);
                weekTime = (WheelView) pickview_week.findViewById(R.id.week_time);
                splitView = (WheelView) pickview_week.findViewById(R.id.split_view);
                weekstartTime = (WheelView) pickview_week.findViewById(R.id.weekstart_time);
                weekstopTime = (WheelView) pickview_week.findViewById(R.id.weekstop_time);
                String currentWeek1 = "";
                String currentWeek2 = "";
                String currentWeek3 = "";
                if (currentTime == null || currentTime == "") {
                    //如果没有给定指定的时间定位  默认为当前时间
                    //设置为当前时间
                    Calendar instance = Calendar.getInstance();
                    String mWay = String.valueOf(instance.get(Calendar.DAY_OF_WEEK));
                    if ("1".equals(mWay)) {
                        mWay = "每周日";
                    } else if ("2".equals(mWay)) {
                        mWay = "每周一";
                    } else if ("3".equals(mWay)) {
                        mWay = "每周二";
                    } else if ("4".equals(mWay)) {
                        mWay = "每周三";
                    } else if ("5".equals(mWay)) {
                        mWay = "每周四";
                    } else if ("6".equals(mWay)) {
                        mWay = "每周五";
                    } else if ("7".equals(mWay)) {
                        mWay = "每周六";
                    }
                    for (int i = 0; i < weeks.length; i++) {
                        if (mWay.equals(weeks[i])) {
                            currentWeek1 = weeks[i];
                            break;
                        }
                    }
                } else {
                    String[] split = currentTime.split("-");
                    if (split[0] != null && split[0] != "") {
                        for (int i = 0; i < weeks.length; i++) {
                            if (split[0].equals(weeks[i])) {
                                currentWeek1 = weeks[i];
                            }
                        }
                        if (split[1] != null && split[1] != "") {
                            for (int i = 0; i < times.length; i++) {
                                if (split[1].equals(times[i])) {
                                    currentWeek2 = times[i];
                                }
                            }
                            if (split[2] != null && split[2] != "") {
                                for (int i = 0; i < times.length; i++) {
                                    if (split[2].equals(times[i])) {
                                        currentWeek3 = times[i];
                                    }
                                }
                            }
                        }
                    }
                }
                weekTime.setCenterItem(setWeek(currentWeek1, weekTime));
                weekstartTime.setCenterItem(setMinutesInterval30(currentWeek2, weekstartTime));
                weekstopTime.setCenterItem(setMinutesInterval30(currentWeek3, weekstopTime));
                splitView.addData("-");
                splitView.setCenterItem(0);
                selectType.addView(pickview_week);
                break;
            case Type_MORETIME:
                View pickview_time4 = LayoutInflater.from(activity).inflate(R.layout.pickview_time4, null);
                time4StartTime1 = (WheelView) pickview_time4.findViewById(R.id.time4_start_Time1);
                time4SplitView1 = (WheelView) pickview_time4.findViewById(R.id.time4_split_view1);
                time4StopTime1 = (WheelView) pickview_time4.findViewById(R.id.time4_stop_Time1);
                time4StartTime2 = (WheelView) pickview_time4.findViewById(R.id.time4_start_Time2);
                time4SplitView2 = (WheelView) pickview_time4.findViewById(R.id.time4_split_view2);
                time4StopTime2 = (WheelView) pickview_time4.findViewById(R.id.time4_stop_Time2);
                String currentStartTime1 = "";
                String currentStartTime2 = "";
                String currentStopTime1 = "";
                String currentStopTime2 = "";
                if (currentTime != null && currentTime != "") {
                    String[] split = currentTime.split("-");
                    if (split[0] != "" && split[0] != null) {
                        String[] split1 = split[0].split(":");
                        currentStartTime1 = split1[0];
                        currentStopTime1 = split1[1];

                    }
                    if (split[1] != "" && split[1] != null) {
                        String[] split2 = split[1].split(":");
                        currentStartTime2 = split2[0];
                        currentStopTime2 = split2[1];
                    }
                }
                time4StartTime1.setCenterItem(setHouts(currentStartTime1, time4StartTime1));
                time4StopTime1.setCenterItem(setMinutes(currentStopTime1, time4StopTime1));
                time4StartTime2.setCenterItem(setHouts(currentStartTime2, time4StartTime2));
                time4StopTime2.setCenterItem(setMinutes(currentStopTime2, time4StopTime2));
                time4SplitView1.addData(":");
                time4SplitView2.addData(":");
                time4SplitView1.setCenterItem(0);
                time4SplitView2.setCenterItem(0);
                selectType.addView(pickview_time4);
                break;
            case Type_NORMAL:
                ArrayList<String> strings_normal = (ArrayList<String>) map.get("data");
                if (strings_normal == null) {
                    return;
                }
                View pickview_normal = LayoutInflater.from(activity).inflate(R.layout.pickview_normal, null);
                normalTime = (WheelView) pickview_normal.findViewById(R.id.normal_time);
                int p = 0;
                for (int i = 0; i < strings_normal.size(); i++) {
                    normalTime.addData(strings_normal.get(i));
                    if (currentTime.equals(strings_normal.get(i)))p = i;
                }
                normalTime.setCenterItem(p);
                selectType.addView(pickview_normal);
                break;
            case Type_DATATIME:
                ArrayList<String> strings = (ArrayList<String>) map.get("data");
                View pickview_datatime = LayoutInflater.from(activity).inflate(R.layout.pickview_datatime, null);
                dataTime = (WheelView) pickview_datatime.findViewById(R.id.data_time);
                datastartTime = (WheelView) pickview_datatime.findViewById(R.id.datastart_time);
                datastopTime = (WheelView) pickview_datatime.findViewById(R.id.datastop_time);
                datasplitView = (WheelView) pickview_datatime.findViewById(R.id.datasplit_view);
                ArrayList<String> strings1 = new ArrayList<>();
                if (strings == null) {
                    Toast.makeText(activity, "时间日期异常", Toast.LENGTH_SHORT).show();
                    return;
                }
                for (int i = 0; i < strings.size(); i++) {
                    if (i == 0) {
                        dataTime.addData("今天");
                        strings1.add("今天");
                    } else {
                        dataTime.addData(strings.get(i));
                        strings1.add(strings.get(i));
                    }

                }
                dataTime.setCenterItem(0);
                datasplitView.addData(":");
                datasplitView.setCenterItem(0);
                if (currentTime != null && !currentTime.isEmpty()) {
                    String[] split = currentTime.split("-");
                    String dataTimeStr = split[0];
                    String[] split1 = split[1].split(":");

                    String[] monthss = dataTimeStr.split("月");
                    int i1 = Integer.valueOf(monthss[0]).intValue();
                    String[] dayss = monthss[1].split("日");
                    int j1 = Integer.valueOf(dayss[0]).intValue();
                    String defaultTime = i1 + "月" + j1 + "日";
                    for (int i = 0; i < strings1.size(); i++) {
                        if (defaultTime.equals(strings1.get(i))) {
                            dataTime.setCenterItem(i);
                        }
                    }
                    datastartTime.setCenterItem(setHouts(split1[0], datastartTime));
                    datastopTime.setCenterItem(setMinutes(split1[1], datastopTime));
                } else {
                    datastartTime.setCenterItem(setHouts("", datastartTime));
                    datastopTime.setCenterItem(setMinutes("", datastopTime));
                }

                selectType.addView(pickview_datatime);
                break;
            case Type_TIMEINTERVAL:
                View pickview_timeinterval = LayoutInflater.from(activity).inflate(R.layout.pickview_timeinterval, null);
                intervalstartTime = (WheelView) pickview_timeinterval.findViewById(R.id.intervalstart_time);
                intervalsplitView = (WheelView) pickview_timeinterval.findViewById(R.id.intervalsplit_view);
                intervalstopTime = (WheelView) pickview_timeinterval.findViewById(R.id.intervalstop_time);
                intervalstartTime.setCenterItem(setMinutesInterval30(12, 45, "", intervalstartTime));
                intervalstopTime.setCenterItem(setMinutesInterval30(12, 45, "", intervalstopTime));
                intervalsplitView.addData("至");
                intervalsplitView.setCenterItem(0);
                selectType.addView(pickview_timeinterval);
                break;
        }

    }


    void refreshTimePosition(boolean isCurrentYear, boolean isCurrentMonth, boolean isCurrentDay, boolean isCurrentHour, boolean isCurrentMinute,
                             boolean isResetYear, boolean isResetMonth, boolean isResetDay, boolean isResetHour, boolean isResetMinute,
                             WheelView... wheelViews) {
        for (int i = 0; i < wheelViews.length; i++) {
            wheelViews[i].clearData();
        }
        getCurrentTime();
        if (isCurrentYear && isResetYear) {
            timeYear.setCenterItem(setYear(1970, year, year, timeYear));
        }

        if (isResetMonth) {
            if (isCurrentMonth) {
                timeMonth.setCenterItem(setMonth(1, month, month, timeMonth));
            } else {
                timeMonth.setCenterItem(setMonth(1, 1, 12, timeMonth));
            }
        }

        if (isResetDay) {
            if (isCurrentDay) {
                timeDay.setCenterItem(setDay(1, day, day, timeDay));
            } else {
                //获取当前年y
                //获取当前月
                String ycenterItem = (String) timeYear.getCenterItem();
                String[] yArray = ycenterItem.split("年");
                int yi = Integer.valueOf(yArray[0]).intValue();

                boolean isFour = yi % 4 == 0 ? true : false;

                //获取当前月
                String mcenterItem = (String) timeMonth.getCenterItem();
                String[] monthArray = mcenterItem.split("月");
                int mi = Integer.valueOf(monthArray[0]).intValue();
                if (isFour && mi == 2) {
                    timeDay.setCenterItem(setDay(1, 1, 29, timeDay));
                } else if (mi == 2) {
                    timeDay.setCenterItem(setDay(1, 1, 28, timeDay));
                } else if (mi == 1 || mi == 3 || mi == 5 || mi == 7 || mi == 8 || mi == 10 || mi == 12) {
                    timeDay.setCenterItem(setDay(1, 1, 31, timeDay));
                } else {
                    timeDay.setCenterItem(setDay(1, 1, 30, timeDay));
                }
            }
        }

        if (isResetHour) {
            if (isCurrentHour) {
                timeHour.setCenterItem(setHour(00, hour, hour, timeHour));
            } else {
                timeHour.setCenterItem(setHour(00, 00, 23, timeHour));
            }
        }

        if (isResetMinute) {
            if (isCurrentMinute) {
                timeMinutes.setCenterItem(setMinutes(00, minute, minute, timeMinutes));
            } else {
                timeMinutes.setCenterItem(setMinutes(00, 00, 59, timeMinutes));
            }

        }


        for (int i = 0; i < wheelViews.length; i++) {
            wheelViews[i].notifyDataSetChanged();
        }


    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void pickViewShow(View view) {
        // 实例化一个ColorDrawable颜色为半透明
        // 设置弹出窗体的背景
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.popwindow_anim_style);
        popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindow.setContentView(popupWindowView);
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        setBackgroundAlpha(0.5f);
    }


//    public void getSureOnClick(PickViewCallback pickViewCallback) {
//        pickviewSure.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                try {
//                    String sureResult = getSureResult(TYPE);
//                    pickViewCallback.getSureResult(sureResult);
//                    popupWindow.dismiss();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//            }
//        });
//    }

    private String getSureResult(String type) throws Exception {
        String result = "";
        String commondResult;
        switch (type) {
            case Type_TIME_HM: {
                String yearString = getWheelView(timeYear).replace("年", "");
                String monthString = getWheelView(timeMonth).replace("月", "");
                String dayString = getWheelView(timeDay).replace("日", "");
                String hourString = getWheelView(timeHour).replace("时", "");
                String minutesString = getWheelView(timeMinutes).replace("分", "");
                JSONObject weekJsonObject = new JSONObject();
                weekJsonObject.put(MARK, yearString + "-" + monthString + "-" + dayString + " " + hourString + ":" + minutesString);
                weekJsonObject.put(title, yearString + "-" + monthString + "-" + dayString + " " + hourString + ":" + minutesString);
                JSONObject weekJson = new JSONObject();
                weekJson.put(selInfo, weekJsonObject);
                weekJson.put(title, getTitleString());
                result = weekJson.toString();
            }
            break;
            case Type_WEEKTIME:
                commondResult = getCommondResult(getWeekTimeString(), getStartTimeString(), getEndTimeString());
                JSONObject weekJsonObject = new JSONObject();
                weekJsonObject.put(MARK, commondResult);
                weekJsonObject.put(title, commondResult);
                JSONObject weekJson = new JSONObject();
                weekJson.put(selInfo, weekJsonObject);
                weekJson.put(title, getTitleString());
                result = weekJson.toString();
                break;
            case Type_MORETIME:
                commondResult = getCommondResult((String) time4StartTime1.getCenterItem() + ":" + (String) time4StopTime1.getCenterItem(), (String) time4StartTime2.getCenterItem() + ":" + (String) time4StopTime2.getCenterItem());
                JSONObject time4JsonObject = new JSONObject();
                time4JsonObject.put(MARK, commondResult);
                time4JsonObject.put(title, commondResult);
                JSONObject time4Json = new JSONObject();
                time4Json.put(selInfo, time4JsonObject);
                time4Json.put(title, getTitleString());
                result = time4Json.toString();
                break;
            case Type_DATATIME:
                Calendar selectedDate = Calendar.getInstance();//系统时间   有mark 为标记时间  无mark 为当前时间
                int month = selectedDate.get(Calendar.MONTH);
                int day = selectedDate.get(Calendar.DAY_OF_MONTH);

                commondResult = getCommondResult("今天".equals((String) dataTime.getCenterItem()) ? (month + 1) + "月" + day + "日" : (String) dataTime.getCenterItem(), (String) datastartTime.getCenterItem() + ":" + (String) datastopTime.getCenterItem());
                JSONObject dataJsonObject = new JSONObject();
                dataJsonObject.put(MARK, commondResult);
                dataJsonObject.put(title, commondResult);
                JSONObject dataJson = new JSONObject();
                dataJson.put(selInfo, dataJsonObject);
                dataJson.put(title, getTitleString());
                result = dataJson.toString();
                break;
            case Type_NORMAL:
                commondResult = getCommondResult((String) normalTime.getCenterItem());
                JSONObject normalJsonObject = new JSONObject();
                normalJsonObject.put(MARK, commondResult);
                normalJsonObject.put(title, commondResult);
                JSONObject normalJson = new JSONObject();
                normalJson.put(selInfo, normalJsonObject);
                normalJson.put(title, getTitleString());
                result = normalJson.toString();
                break;
            case Type_TIMEINTERVAL:
                commondResult = getCommondResult((String) intervalstartTime.getCenterItem(), (String) intervalstopTime.getCenterItem());
                JSONObject IntervalJsonObject = new JSONObject();
                IntervalJsonObject.put(MARK, commondResult);
                IntervalJsonObject.put(title, commondResult);
                JSONObject IntervalJson = new JSONObject();
                IntervalJson.put(selInfo, IntervalJsonObject);
                IntervalJson.put(title, getTitleString());
                result = IntervalJson.toString();
                break;
            case Type_TIME:
                commondResult = getCommondResult((String) yearTime.getCenterItem(), (String) monthTime.getCenterItem(), (String) dayTime.getCenterItem());
                JSONObject timeJsonObject = new JSONObject();
                timeJsonObject.put(MARK, commondResult);
                timeJsonObject.put(title, commondResult);
                JSONObject timeJson = new JSONObject();
                timeJson.put(selInfo, timeJson);
                timeJson.put(title, getTitleString());
                result = timeJson.toString();
                break;
        }

        return result;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pickview_cancel:
                //取消点击
                popupWindow.dismiss();
                break;
        }
    }


    private String getCommondResult(String... strings) {
        StringBuffer stringBuffer = new StringBuffer();

        for (int i = 0; i < strings.length; i++) {
            if (i == 0) {
                stringBuffer.append(strings[i]);
            } else {
                stringBuffer.append("-" + strings[i]);
            }
        }
        return stringBuffer.toString();
    }

    private String getTitleString() {
        String trim = pickviewTitle.getText().toString().trim();
        return trim;
    }

    private String getWeekTimeString() {
        return (String) weekTime.getCenterItem();
    }

    private String getStartTimeString() {
        return (String) weekstartTime.getCenterItem();
    }

    private String getEndTimeString() {
        return (String) weekstopTime.getCenterItem();
    }

    private String getWheelView(WheelView wheelView) {
        return (String) wheelView.getCenterItem();
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void showPickViewAndTYPE(Activity activity, String type, String title, String cancelText, String sureText, String currentTime, Map<String, Object> map, View decorView, PickViewCallback pickViewCallback) {
        selectPickViewType(activity, type, currentTime, map);
        pickViewShow(decorView);
        setPickviewTitle(title);
        setPickviewCancel(cancelText);
        setPickviewSure(sureText);
        pickviewSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    pickViewCallback.getSureResult(getSureResult(type));
                    popupWindow.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //设置时间 -- 星期几
    private int setWeek(String currentWeek, WheelView... wheelViews) {
        int index = 0;
        int positionIndex = 0;
        for (int i = 0; i < weeks.length; i++) {
            if (currentWeek.equals(weeks[i])) {
                positionIndex = index;
            } else {
                index++;
            }
            for (int j = 0; j < wheelViews.length; j++) {
                wheelViews[j].addData(weeks[i]);
            }
        }
        return positionIndex;
    }

    //设置时间 -- 分钟
    private int setMinutes(String currentMinutes, WheelView... wheelViews) {
        int index = 0;
        int positionIndex = 0;
        for (int i = 0; i < 60; i++) {
            String addDataString = "";
            if (i < 10) {
                addDataString = "0" + i;
            } else {
                addDataString = i + "";
            }
            //判断当前时间
            if (!currentMinutes.equals(addDataString)) {
                index++;
            } else {
                positionIndex = index;
            }
            for (int j = 0; j < wheelViews.length; j++) {
                wheelViews[j].addData(addDataString);

            }
        }
        return positionIndex;
    }

    //设置时间 -- 小时
    private int setHouts(String currentHours, WheelView... wheelViews) {
        int index = 0;
        int positionIndex = 0;
        for (int i = 0; i < 24; i++) {
            String addDataString = "";
            if (i < 10) {
                addDataString = "0" + i;
            } else {
                addDataString = i + "";
            }
            if (!currentHours.equals(addDataString)) {
                index++;
            } else {
                positionIndex = index;
            }
            for (int j = 0; j < wheelViews.length; j++) {
                wheelViews[j].addData(addDataString);
            }
        }
        return positionIndex;
    }

    //设置时间节点 -- 30分钟节点时间
    private int setMinutesInterval30(String currentTime, WheelView... wheelViews) {
        int index = 0;
        int positionIndex = 0;
        for (int i = 0; i < times.length; i++) {
            if (currentTime.equals(times[i])) {
                positionIndex = index;
            } else {
                index++;
            }
            for (int k = 0; k < wheelViews.length; k++) {
                wheelViews[k].addData(times[i]);
            }
        }
        return positionIndex;
    }

    //设置时间节点 -- 30分钟节点时间
    private int setMinutesInterval30(int startIndex, int endIndex, String currentTime, WheelView... wheelViews) {
        int index = 0;
        int positionIndex = 0;
        for (int i = startIndex; i < endIndex; i++) {
            if (currentTime.equals(times[i])) {
                positionIndex = index;
            } else {
                index++;
            }
            for (int k = 0; k < wheelViews.length; k++) {
                wheelViews[k].addData(times[i]);
            }
        }
        return positionIndex;
    }
    //设置年月日


    private int setYear(int minYear, int currentYear, int maxYear, WheelView wheelView) {
        int index = 0;
        int positionYear = 0;
        for (int i = minYear; i <= maxYear; i++) {
            wheelView.addData(i + "年");
            index++;
            if (i == currentYear) {
                positionYear = index;
            }
        }
        return positionYear - 1;
    }

    private int setMonth(int minMonth, int currentMonth, int maxMonth, WheelView wheelView) {
        int index = 0;
        int positionMonth = 0;
        for (int i = minMonth; i <= maxMonth; i++) {
            wheelView.addData(String.format("%02d", i) + "月");
            index++;
            if (i == currentMonth) {
                positionMonth = index;
            }
        }
        return positionMonth - 1;
    }

    private int setDay(int minDay, int currentDay, int maxDay, WheelView wheelView) {
        int index = 0;
        int positionDay = 0;
        for (int i = minDay; i <= maxDay; i++) {
            wheelView.addData(String.format("%02d", i) + "日");
            index++;
            if (i == currentDay) {
                positionDay = index;
            }
        }
        return positionDay - 1;
    }

    private int setHour(int minHour, int currentHour, int maxHour, WheelView wheelView) {
        int index = 0;
        int positionHour = 0;
        for (int i = minHour; i <= maxHour; i++) {
            wheelView.addData(String.format("%02d", i) + "时");
            index++;
            if (i == currentHour) {
                positionHour = index;
            }
        }
        return positionHour - 1;
    }

    private int setMinutes(int minMinutes, int currentMinutes, int maxMinutes, WheelView wheelView) {
        int index = 0;
        int positionMinutes = 0;
        for (int i = minMinutes; i <= maxMinutes; i++) {
            wheelView.addData(String.format("%02d", i) + "分");
            index++;
            if (i == currentMinutes) {
                positionMinutes = index;
            }
        }
        return positionMinutes - 1;
    }

    private int setSeconds(int minSeconds, int currentSeconds, int maxSeconds, WheelView wheelView) {
        int index = 0;
        int positionSeconds = 0;
        for (int i = minSeconds; i <= maxSeconds; i++) {
            wheelView.addData(i + "秒");
            index++;
            if (i == currentSeconds) {
                positionSeconds = index;
            }
        }
        return positionSeconds - 1;
    }

//    private int setMonth(String currentTime, WheelView... wheelViews) {
//        long l = System.currentTimeMillis();
//        SimpleDateFormat monthDateFormat = new SimpleDateFormat("MM");
//        String currentmonth = monthDateFormat.format(l);
//        ALog.i("pickview", "setYearMonthDay: " + currentmonth);
//        int index = 0;
//        int positionIndex = 0;
//        currentMonthInt = Integer.valueOf(currentmonth).intValue();
//        for (int i = 1; i <= 12; i++) {
//            for (int j = 0; j < wheelViews.length; j++) {
//                if (i < 10) {
//                    wheelViews[j].addData("0" + i + "");
//                } else {
//                    wheelViews[j].addData(i + "");
//                }
//            }
//            index++;
//            if (currentMonthInt == i) {
//                positionIndex = index;
//            }
//        }
//        return positionIndex - 1;
//    }

//    private int setday(String currentTime, WheelView... wheelViews) {
//        long l = System.currentTimeMillis();
//        SimpleDateFormat dayDateFormat = new SimpleDateFormat("dd");
//        String currentday = dayDateFormat.format(l);
//        ALog.i("pickview", "setYearMonthDay: " + currentday);
//        currentDayInt = Integer.valueOf(currentday).intValue();
//        int index = 0;
//        int positionIndex = 0;
//        if (currentYearInt % 4 == 0) {
//            if (currentMonthInt == 2) {
//                daycount = 29;
//            } else if (currentMonthInt == 1
//                    || currentMonthInt == 3
//                    || currentMonthInt == 5
//                    || currentMonthInt == 7
//                    || currentMonthInt == 8
//                    || currentMonthInt == 10
//                    || currentMonthInt == 12
//            ) {
//                daycount = 31;
//            } else {
//                daycount = 30;
//            }
//        } else {
//            if (currentMonthInt == 2) {
//                daycount = 28;
//            } else if (currentMonthInt == 1
//                    || currentMonthInt == 3
//                    || currentMonthInt == 5
//                    || currentMonthInt == 7
//                    || currentMonthInt == 8
//                    || currentMonthInt == 10
//                    || currentMonthInt == 12
//            ) {
//                daycount = 31;
//            } else {
//                daycount = 30;
//            }
//        }
//        for (int i = 0; i < wheelViews.length; i++) {
//            wheelViews[i].clearData();
//        }
//        for (int i = 1; i <= daycount; i++) {
//            for (int j = 0; j < wheelViews.length; j++) {
//                if (i < 10) {
//                    wheelViews[j].addData("0" + i + "");
//                } else {
//                    wheelViews[j].addData(i + "");
//                }
//            }
//            index++;
//            if (currentDayInt == i) {
//                positionIndex = index;
//            }
//            ALog.i("setday", "setday: " + positionIndex);
//        }
//
//        return positionIndex - 1;
//
//    }


}

